﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SouthApps.MobileHealth.SDK.DAL.Model;

namespace SouthApps.MobileHealth.SDK.DAL.DataAccess
{
    public class ServicioGuardiaDao : DataAccessObject
    {
        #region Get

        public ServicioGuardia GetServicioGuardia(int servicioGuardiaId)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    return entities.servicioguardia.FirstOrDefault(e =>
                        e.servicioGuardiaId == servicioGuardiaId);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        public List<ServicioGuardia> GetServiciosGuardiaFinanciador(int financiadorId)
        {
            return this.GetServiciosGuardiaFinanciador(financiadorId, true);
        }

        public List<ServicioGuardia> GetServiciosGuardiaFinanciador(int financiadorId, bool activo)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    var query = from e in entities.servicioguardia
                                where e.financiadorId == financiadorId
                                select e;

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Add

        public void AddServicioGuardia(ServicioGuardia servicioGuardia)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    entities.AddToservicioguardia(servicioGuardia);
                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion

        #region Modify

        public void ModifyServicioGuardia(ServicioGuardia servicioGuardia)
        {
            try
            {
                using (var entities = this.EntityContext)
                {
                    ServicioGuardia oldServicioGuardia = entities.servicioguardia.FirstOrDefault(e => e.servicioGuardiaId == servicioGuardia.servicioGuardiaId);
                    oldServicioGuardia = servicioGuardia;

                    this.SaveChanges(entities);
                }
            }
            catch (Exception ex)
            {
                throw this.HandleException(ex);
            }
        }

        #endregion
    }
}
